﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using ITProject.Domain.Entities;

namespace ITProject.WebUI.Models.ProjectUser
{
    public class ProjectUserChangeMyTaskStatusViewModel
    {
        [Display(Name = "Nazwa zadania")]
        public string Name { get; set; }
        public List<SelectListItem> StatusList { get; set; }
        [Display(Name = "Nowy status zadania")]
        [Required(ErrorMessage = "Musisz podać nowy status")]
        public string ReturnStatus { get; set; }
        [Display(Name = "Komentarz zmiany statusu")]
        public string Comment { get; set; }

        // ukryte
        public string ProjectName { get; set; }
        public int TaskId { get; set; }

        public static void CompleteStatusList(ProjectUserChangeMyTaskStatusViewModel vm, byte status)
        {
            string actualStatus = StatusName.ConvertFromByte(status);
            SelectListItem actualStatusItem = new SelectListItem { Selected = true, Text = actualStatus };
            vm.StatusList.Add(actualStatusItem);
            if (actualStatus != StatusName.Planned)
                vm.StatusList.Add(new SelectListItem { Selected = false, Text = StatusName.Planned });
            if (actualStatus != StatusName.Active)
                vm.StatusList.Add(new SelectListItem { Selected = false, Text = StatusName.Active });
            if (actualStatus != StatusName.Done)
                vm.StatusList.Add(new SelectListItem { Selected = false, Text = StatusName.Done });
            if (actualStatus != StatusName.Tested)
                vm.StatusList.Add(new SelectListItem { Selected = false, Text = StatusName.Tested });
        }

        public static ProjectUserChangeMyTaskStatusViewModel Convert(Task task, string projectName)
        {
            ProjectUserChangeMyTaskStatusViewModel vm = new ProjectUserChangeMyTaskStatusViewModel();
            vm.ProjectName = projectName;
            vm.TaskId = task.Id;
            vm.Name = task.Name;
            vm.StatusList = new List<SelectListItem>();
            CompleteStatusList(vm, task.Status);
            return vm;
        }

        public static StatusChange Convert(ProjectUserChangeMyTaskStatusViewModel vm, byte oldStatus, int userId)
        {
            StatusChange sc = new StatusChange();
            sc.Comment = vm.Comment;
            sc.DateOfChange = DateTime.Now;
            sc.NewStatus = (byte)StatusName.ConvertFromString(vm.ReturnStatus);
            sc.OldStatus = oldStatus;
            sc.UserId = userId;
            return sc;
        }
    }
}